
#include "sec_common.h"
#include "e_os.h"
#include "hard.h"

unsigned char aes_key[] = {
		/* KEY 128 bit */
		0xAF,0x86,0x18,0x23,0x8C,0x94,0xA1,0x19,0xAE,0x6D,0xE9,0x22,0xDB,0xB9,0x35,0x4D,
		/* KEY 192 bit */
		0x9F,0xCE,0xB8,0x96,0x34,0x5C,0xE3,0x7C,0x3F,0xFE,0x54,0xEA,0xD1,0x15,0x28,0xED,
		0x87,0x1D,0x0F,0x6E,0x93,0xCC,0xFD,0x54,
		/* KEY 256 bit */
		0xE3,0x83,0x0F,0x2F,0xF5,0x87,0x7D,0xAD,0x19,0x41,0x75,0x7B,0x17,0xB5,0xE3,0x92,
		0xB5,0x1D,0x7D,0x21,0x15,0xE7,0x0F,0x8B,0x6E,0x93,0x91,0x98,0xEB,0x6F,0xDE,0x4B};

unsigned char aes_iv[] = {
		/* IV for 128/192/256 bit CTR */
		0x38,0x32,0xF2,0x7E,0x0A,0x14,0x51,0xAF,0x4B,0x97,0x47,0xA0,0x17,0x7C,0xDF,0xC2};

unsigned char aes_data[] = {
		/* DATA for 128/192/256 bit ecb */
		0xF7,0xAF,0x83,0xD4,0x98,0x7A,0x94,0x83,0x98,0xEE,0x24,0x8A,0xD8,0xB7,0x09,0x95,
		0xF7,0xAF,0x83,0xD4,0x98,0x7A,0x94,0x83,0x98,0xEE,0x24,0x8A,0xD8,0xB7,0x09,0x95,
		/* DATA for 128/192/256 bit CTR */
		0x63,0x36,0x3D,0x95,0xAC,0x94,0x46,0x28,0x29,0x2D,0xB1,0x2F,0x6B,0x39,0x37,0x5C,
		0x66,0x17,0x94,0x6A,0xFC,0xC2,0x73,0x14,0xE5,0xA9,0x4B,0x59,0xA1,0x98,0xCC,0x33};

unsigned char aes_out[] = {
		/* STD OUT for 128 bit ecb */
		0xa9,0x18,0x0e,0x6c,0xf0,0x2c,0x4a,0xad,0xe2,0x57,0x3b,0xc1,0x50,0x9e,0x26,0x41,
		/* STD OUT for 192 bit ecb */
		0x96,0x96,0x4f,0x1f,0x4b,0x3b,0x1e,0x3d,0x70,0xf4,0x6e,0x20,0xad,0xfc,0x1c,0x40,
		/* STD OUT for 256 bit ecb */
		0x3f,0x51,0x8e,0xc6,0x4c,0x33,0xb9,0x33,0x50,0x0e,0xf6,0x45,0x12,0x64,0x8c,0xff,
		/* STD OUT for 128 bit CTR */
		0x4b,0x03,0x7c,0x8f,0xf7,0x09,0x07,0x92,0x94,0x4b,0x60,0xff,0x19,0x4c,0x86,0x93,
		0xe9,0x39,0x8f,0x33,0xd9,0x9d,0xe2,0x74,0xd6,0xc7,0x9c,0xe5,0x18,0x4f,0x6a,0xcd,
		/* STD OUT for 192 bit CTR */
		0x80,0xd5,0xe7,0xed,0xd2,0x00,0xc4,0xd8,0xea,0x78,0x5f,0x28,0x43,0xaa,0xb1,0x14,
		0x5a,0x96,0xcc,0x0f,0x6a,0xa9,0x44,0x1f,0x11,0x0d,0x65,0x09,0xb0,0x19,0xc7,0x57,
		/* STD OUT for 256 bit CTR */
		0x6f,0x8e,0x07,0x76,0x8a,0x7d,0xd4,0xe8,0xa7,0x84,0x18,0xed,0xa0,0xbc,0xae,0x72,
		0x9c,0xcb,0x3e,0xfe,0xe0,0x4d,0x30,0x88,0xd0,0xbf,0xf8,0x8c,0xe4,0x12,0x77,0xa4};


aes_ctr_ssl_hard_test(int fd,unsigned char key_size)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,*dt,iv_tmp[32];
	unsigned int i;

	capi.alg = AES;
	capi.type  = CTR;
	capi.as = INITFINAL;
	capi.key_len = key_size;
	capi.iv_len = 16*2;
	capi.data_len = (( 0x10|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(capi.data_len);
	dt = malloc(capi.data_len);


	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

	memcpy((void *)iv_tmp,(void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	memcpy((void *)dt,(void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before ctr(aes128) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter ctr(aes128) hard enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
/*
	debug("\nafter ctr(aes128) soft enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",out[i]);
	}
	debug("\n");
*/
#endif


/////////////////////////////////////////////////////////////////////////////
//dec
/////////////////////////////////////////////////////////////////////////////

	capi.opt = DEC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);
	memcpy((void *)(mem+CHEAD_SIZE+capi.key_len),(void *)iv_tmp,capi.iv_len);

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

	if(!memcmp((void *)dt,(void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len))
		debug("aes_ctr_ssl_hard key:0x%x,  dec test ok!\n",key_size);
	else
	{
		errout("aes_ctr_ssl_hard key:0x%x,  dec test error!\n",key_size);
		fail_prosess();
	}

	free((void *)out);
	free((void *)mem);
	free((void *)dt);

}
aes_cbc_ssl_hard_test(int fd,unsigned char key_size)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,*dt,iv_tmp[16];
	unsigned int i;

	capi.alg = AES;
	capi.type  = CBC;
	capi.as = INITFINAL;
	capi.key_len = key_size;
	capi.iv_len = 16;
	capi.data_len = (( 0x10|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(capi.data_len);
	dt = malloc(capi.data_len);


	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

	memcpy((void *)iv_tmp,(void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	memcpy((void *)dt,(void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before cbc(aes128) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter cbc(aes128) hard enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
/*
	debug("\nafter cbc(aes128) soft enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",out[i]);
	}
	debug("\n");
*/
#endif


/////////////////////////////////////////////////////////////////////////////
//dec
/////////////////////////////////////////////////////////////////////////////

	capi.opt = DEC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);
	memcpy((void *)(mem+CHEAD_SIZE+capi.key_len),(void *)iv_tmp,capi.iv_len);

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

	if(!memcmp((void *)dt,(void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len))
		debug("aes_cbc_ssl_hard key:0x%x,  dec test ok!\n",key_size);
	else
	{
		errout("aes_cbc_ssl_hard key:0x%x,  dec test error!\n",key_size);
		fail_prosess();
	}

	free((void *)out);
	free((void *)mem);
	free((void *)dt);

}

aes_ofb_ssl_hard_test(int fd,unsigned char key_size)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,*dt,iv_tmp[16];
	unsigned int i;

	capi.alg = AES;
	capi.type  = OFB;
	capi.as = INITFINAL;
	capi.key_len = key_size;
	capi.iv_len = 16;
	capi.data_len = (( 0x10|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(capi.data_len);
	dt = malloc(capi.data_len);


	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

	memcpy((void *)iv_tmp,(void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	memcpy((void *)dt,(void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before ofb(aes128) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter ofb(aes128) hard enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
/*
	debug("\nafter ofb(aes128) soft enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",out[i]);
	}
	debug("\n");
*/
#endif


/////////////////////////////////////////////////////////////////////////////
//dec
/////////////////////////////////////////////////////////////////////////////

	capi.opt = DEC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);
	memcpy((void *)(mem+CHEAD_SIZE+capi.key_len),(void *)iv_tmp,capi.iv_len);

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

	if(!memcmp((void *)dt,(void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len))
		debug("ofb_cbc_ssl_hard key:0x%x,  dec test ok!\n",key_size);
	else
	{
		errout("ofb_cbc_ssl_hard key:0x%x,  dec test error!\n",key_size);
		fail_prosess();
	}

	free((void *)out);
	free((void *)mem);
	free((void *)dt);

}
aes_cfb_ssl_hard_test(int fd,unsigned char key_size)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,*dt,iv_tmp[16];
	unsigned int i;

	capi.alg = AES;
	capi.type  = CFB;
	capi.as = INITFINAL;
	capi.key_len = key_size;
	capi.iv_len = 16;
	capi.data_len = (( 0x10|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(capi.data_len);
	dt = malloc(capi.data_len);


	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

	memcpy((void *)iv_tmp,(void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	memcpy((void *)dt,(void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before cfb(aes128) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter cfb(aes128) hard enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
/*
	debug("\nafter cfb(aes128) soft enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",out[i]);
	}
	debug("\n");
*/
#endif


/////////////////////////////////////////////////////////////////////////////
//dec
/////////////////////////////////////////////////////////////////////////////

	capi.opt = DEC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);
	memcpy((void *)(mem+CHEAD_SIZE+capi.key_len),(void *)iv_tmp,capi.iv_len);

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

	if(!memcmp((void *)dt,(void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len))
		debug("aes_cfb_ssl_hard key:0x%x,  dec test ok!\n",key_size);
	else
	{
		errout("aes_cfb_ssl_hard key:0x%x,  dec test error!\n",key_size);
		fail_prosess();
	}

	free((void *)out);
	free((void *)mem);
	free((void *)dt);

}
aes_ecb_ssl_hard_test(int fd,unsigned char key_size)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,*dt,iv_tmp[16];
	unsigned int i;

	capi.alg = AES;
	capi.type  = ECB;
	capi.as = INITFINAL;
	capi.key_len = key_size;
	capi.iv_len = 16;
	capi.data_len = (( 0x10|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);
	out = malloc(capi.data_len);
	dt = malloc(capi.data_len);


	set_rand_mem((void *)(mem+CHEAD_SIZE),capi.key_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

	memcpy((void *)dt,(void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before ecb(aes128) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter ecb(aes128) hard enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
/*
	debug("\nafter ecb(aes128) soft enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",out[i]);
	}
	debug("\n");
*/
#endif


/////////////////////////////////////////////////////////////////////////////
//dec
/////////////////////////////////////////////////////////////////////////////

	capi.opt = DEC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

	if(!memcmp((void *)dt,(void *)(mem+CHEAD_SIZE+capi.key_len+capi.iv_len),capi.data_len))
		debug("aes_ecb_ssl_hard key:0x%x,  dec test ok!\n",key_size);
	else
	{
		errout("aes_ecb_ssl_hard key:0x%x,  dec test error!\n",key_size);
		fail_prosess();
	}

	free((void *)out);
	free((void *)mem);
	free((void *)dt);

}

aes_cbc_ssl_hard_externkey_test(int fd,unsigned char key_size)
{
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len;
	unsigned char *out,*dt,iv_tmp[16],key_tmp[32];
	unsigned int i;
	unsigned *space32;

	capi.alg = AES;
	capi.type  = CBC;
	capi.as = INITFINAL;
	capi.key_len = key_size | 0x80000000;
	capi.iv_len = 16;
	capi.data_len = (( 0x10|(unsigned)(rand()) )&(max_crypto_len/2-1))&0xfffffff0 ;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	mem = malloc(CHEAD_SIZE+8+capi.iv_len+capi.data_len);
	out = malloc(capi.data_len);
	dt = malloc(capi.data_len);

	space32 = (unsigned *)(mem+CHEAD_SIZE);
	*space32 = 0xe0300000;					//need 16 byte 0~0xf first in cards
	space32++;
	*space32 = 0x80000000;

	for(i=0;i<key_size;i++)
	{
		key_tmp[i]=i;
	}
	set_rand_mem((void *)(mem+CHEAD_SIZE+8),capi.iv_len);
	set_rand_mem((void *)(mem+CHEAD_SIZE+8+capi.iv_len),capi.data_len);

	memcpy((void *)iv_tmp,(void *)(mem+CHEAD_SIZE+8),capi.iv_len);
	memcpy((void *)dt,(void *)(mem+CHEAD_SIZE+8+capi.iv_len),capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

#ifdef DATA_VIEW
	debug("before cbc(aes128) enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
#endif

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+8+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

#ifdef DATA_VIEW
	debug("\nafter cbc(aes128) hard enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",mem[i]);
	}
	debug("\n");
/*
	debug("\nafter cbc(aes128) soft enc:\n");
	for(i=48;i<20+48;i++)
	{
		debug("%x ",out[i]);
	}
	debug("\n");
*/
#endif


/////////////////////////////////////////////////////////////////////////////
//dec
/////////////////////////////////////////////////////////////////////////////

	capi.opt = DEC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);
	memcpy((void *)(mem+CHEAD_SIZE+8),(void *)iv_tmp,capi.iv_len);

	ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+8+capi.iv_len+capi.data_len),mem);
	while(!(mem[3]&DDONE_MASK));

	if(!memcmp((void *)dt,(void *)(mem+CHEAD_SIZE+8+capi.iv_len),capi.data_len))
		debug("aes_cbc_ssl_hard key:0x%x,  dec test ok!\n",key_size);
	else
	{
		errout("aes_cbc_ssl_hard key:0x%x,  dec test error!\n",key_size);
		fail_prosess();
	}

	free((void *)out);
	free((void *)mem);
	free((void *)dt);

}

aes_cbc_ssl_hard_key32_speed(int fd)
{
	int tcnt=100,MNUM=1000;
	volatile unsigned char *mem;
	struct cipher_api capi;
	unsigned int max_len = max_crypto_len; //16k
	unsigned char *out,*dt,iv_tmp[16];
	unsigned int i;
	struct timeval tv1,tv2;

	int cpid;
	int semval;
	int time,speed;

	capi.alg = AES;
	capi.type  = CBC;
	capi.as = INITFINAL;
	capi.key_len = 32;
	capi.iv_len = 16;
	capi.data_len = (max_crypto_len-100)&0xfffffff0;

	debug("max_crypto_len is 0x%x,data_len is 0x%x,key_size is 0x%x\n",max_len,capi.data_len,capi.key_len);

	for(i=0;i<tcnt-1;i++)
	{
		cpid = fork();
		if(cpid==0)
			break;
	}
//	printf("i = %d\n",i);
	mem= malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);

/////////////////////////////////////////////////////////////////////////////
//enc
/////////////////////////////////////////////////////////////////////////////

	capi.opt = ENC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

	if(cpid==0)
	{
		do{
			sem_getvalue(usem->sem,&semval);
		}
		while(semval==1);
	}else{
		sem_wait(usem->sem);
		gettimeofday(&tv1, NULL);
	}

	for(i=0;i<MNUM;i++)
	{

		ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);

		while(!(mem[3]&DDONE_MASK));
	}

	sem_wait(dsem->sem);

	if(cpid==0)
	{
		free((void *)mem);
		exit(0);
	}else{
		do{
			sem_getvalue(dsem->sem,&semval);
		}
		while(semval!=(MAX_SEM_CNT-tcnt));
		gettimeofday(&tv2, NULL);
		free((void *)mem);
	}
	sem_post(usem->sem);

	for(i=0;i<tcnt;i++)
	{
		sem_post(dsem->sem);
	}

	time = (tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec;

	speed = MNUM*tcnt*capi.data_len/time;
	speed*=8;

	spd_dbg("%s,CBC encrypt data_len %d,tcnt %d,speed is %dMbps,time %d us\n",__FILE__,capi.data_len,tcnt,speed,time);

/////////////////////////////////////////////////////////////////////////////
//dec
/////////////////////////////////////////////////////////////////////////////
	for(i=0;i<tcnt-1;i++)
	{
		cpid = fork();
		if(cpid==0)
			break;
	}

	mem= malloc(CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len);

	capi.opt = DEC;

	memcpy((void *)mem,(void *)(&capi),CHEAD_SIZE);

	if(cpid==0)
	{
		do{
			sem_getvalue(usem->sem,&semval);
		}
		while(semval==1);
	}else{
		sem_wait(usem->sem);
		gettimeofday(&tv1, NULL);
	}

	for(i=0;i<MNUM;i++)
	{
		ioctl(fd,_IOC(0x00,CCP903T_PCIE_MAGIC, ACLASS_CIPHER,CHEAD_SIZE+capi.key_len+capi.iv_len+capi.data_len),mem);

		while(!(mem[3]&DDONE_MASK));
	}
	sem_wait(dsem->sem);

	if(cpid==0)
	{
		free((void *)mem);
		exit(0);
	}else{
		do{
			sem_getvalue(dsem->sem,&semval);
		}
		while(semval!=(MAX_SEM_CNT-tcnt));
		gettimeofday(&tv2, NULL);
		free((void *)mem);
	}

	sem_post(usem->sem);
	for(i=0;i<tcnt;i++)
	{
		sem_post(dsem->sem);
	}

	time = (tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec;

	speed = MNUM*tcnt*capi.data_len/time;
	speed*=8;

	spd_dbg("%s,CBC decrypt data_len %d,tcnt %d,speed is %dMbps,time %d us\n",__FILE__,capi.data_len,tcnt,speed,time);

}

